class EventManager {

    static instance = null;

    static getInstance() {
        if (!EventManager.instance) {
            EventManager.instance = new EventManager();
        }
        return EventManager.instance;
    }

    constructor() {
        this._events = [];
    }

    addEvent(eventName, fn) {
        if (!this._events[eventName]) {
            this._events[eventName] = [];
        }
        this._events[eventName].push(fn);
        // console.log(this._events);
    }

    removeEvent(eventName, fn) {
        let list = this._events[eventName];
        for (let i = 0; i < list.length; i++) {
            if (fn === list[i]) {
                list.splice(i, 1);
            }
        }
        // console.log(this._events);
    }
    emit(eventName, data) {
        let list = this._events[eventName];
        for (let i = 0; i < list.length; i++) {
            list[i](data);
        }
    }
}

export default EventManager;